package com.jg;

import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author: 杨德石
 * @Date: 2020/2/25 16:34
 * @Version 1.0
 */
public class TestMap {

    private HashMap<String, Integer> getMap() {
        HashMap<String, Integer> map = new HashMap<>(16);
        map.put("稽哥", 18);
        map.put("雷哥", 28);
        map.put("吴彦祖", 18);
        map.put("张学友", 40);
        map.put("郭德纲", 50);
        map.put("赵本山", 60);
        map.put("肖战", 29);
        return map;
    }

    /**
     * 分别遍历key和value
     */
    @Test
    public void testMap1() {
        HashMap<String, Integer> map = getMap();
        for (String key : map.keySet()) {
            System.out.println(key);
        }
        for (Integer value : map.values()) {
            System.out.println(value);
        }
    }

    @Test
    public void testIterator() {
        HashMap<String, Integer> map = getMap();
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }

    @Test
    public void testGet() {
        HashMap<String, Integer> map = getMap();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key + ":" + map.get(key));
        }
    }

    @Test
    public void testForeach() {
        HashMap<String, Integer> map = getMap();
        map.forEach((key, value) -> {
            System.out.println(key + ":" + value);
        });
    }

}
